package com.study.example.simpleclient;

import com.sparrow.socket.tcp.codec.RequestDecoder;
import com.sparrow.socket.tcp.codec.ResponseEncoder;
import com.sparrow.socket.util.PacketHelper;
import com.sparrow.common.util.PropertiesUtil;
import com.sparrow.socket.tcp.codec.LengthFieldDecoder;
import com.study.example.simpleclient.net.SimpleClientHandler;
import com.study.example.simpleclient.panel.LoginAuthPanel;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import javax.swing.*;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

/**
 * 简单测试客户端启动类
 */
public class ClientMain {

    public static volatile Channel channel;
    public static volatile JFrame frame;
    public static volatile JPanel currentPanel;

    public static void main(String[] args) {
        PropertiesUtil.load("server.properties");
        connect();

        frame = new JFrame("Java 2D 游戏示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        showPanel(LoginAuthPanel.class);
    }

    public static void showPanel(Class<? extends JPanel> panelClass) {
        JPanel panel = null;
        try {
            panel = panelClass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        showPanel(panel);
    }

    public static void showPanel(JPanel panel) {
        JPanel finalPanel = panel;
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (currentPanel != null) {
                    frame.getContentPane().remove(currentPanel);
                }

                frame.getContentPane().add(finalPanel);
                currentPanel = finalPanel;
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                finalPanel.requestFocus();
            }
        });
    }

    public static void connect() {
        String ip = "localhost";
        int port = PropertiesUtil.getPropertyAsInt("server.port");
        PacketHelper.init("com.sparrow.example_message");
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .remoteAddress(new InetSocketAddress(ip, port))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new ResponseEncoder());
                            pipeline.addLast(new LengthFieldDecoder());
                            pipeline.addLast(new RequestDecoder());
                            pipeline.addLast(new SimpleClientHandler());
                        }
                    });

            ChannelFuture future = bootstrap.connect().sync();
            future.await(20, TimeUnit.SECONDS);

            channel = future.channel();
            System.out.println("Connected to server " + ip + ":" + port);
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }

    public static void showDialog(String msg) {
        JOptionPane.showMessageDialog(currentPanel, msg, msg, JOptionPane.INFORMATION_MESSAGE);
    }
}
